home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MovieToolBox / other QT code not used yet / AddFrameToMovie.c next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  13.0 KB  |  469 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        AddFrameToMovie.                                                    */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demonstrates adding a frame to the end    */
  7. /*            of an existing movie.                                            */
  8. /*                                                                            */
  9. /*        Version:        1.0 Completed 12/2/91                                */
  10. /*                        1.1    Fixed error where ind was assumed to be 0 based    */
  11. /*                            rather than 1 based.  And, added code to update    */
  12. /*                            movie rather than require a flatten movie for    */
  13. /*                            saving the new edited movie.                    */
  14. /*                                                                            */
  15. /*--------------------------------------------------------------------------*/
  16.  
  17. #include     <GestaltEqu.h>
  18. #include    "Movies.h"
  19.  
  20. #define Gestalttest        0xA1AD
  21. #define NoTrap            0xA89F
  22.  
  23. #define    appleID            128            
  24. #define    appleMenu        0
  25. #define    aboutMeCommand    1
  26.  
  27. #define    fileID            129
  28. #define openCommand        1
  29. #define    flattenCommand    2
  30. #define closeCommand    3
  31. #define    quitCommand     5
  32.  
  33. #define    aboutMeDLOG        128
  34. #define    okButton        1
  35.  
  36. #define    MAXWINDOWS        5
  37.  
  38. /*------------------------------------------------------*/
  39. /*    Global Variables.                                    */
  40. /*------------------------------------------------------*/
  41.  
  42. Boolean                DoneFlag = FALSE;
  43. MenuHandle            mymenu0, mymenu1;
  44. Boolean                playingMovie[MAXWINDOWS];
  45. Movie                myMovie[MAXWINDOWS];
  46. WindowPtr            myWindow[MAXWINDOWS];
  47. int                    startlocation;
  48.  
  49. void Error(StringPtr errorMsg, OSErr result)
  50. {    
  51.     if (result) DebugStr(errorMsg);
  52. }
  53.  
  54. /*------------------------------------------------------*/
  55. /*    getMovieFromFile().                                    */
  56. /*------------------------------------------------------*/
  57.  
  58. Movie getMovieFromFile()
  59. {    OSErr                    err;
  60.     StandardFileReply        reply;
  61.     Point                    where = {200, 50};
  62.     SFTypeList                types;
  63.     short                    movieResRefNum;
  64.     short                    actualResId;
  65.     Movie                    theMovie;
  66.     long                    i, maxCompressionSize;
  67.     Track                    myVideoTrack;
  68.     Media                    myVideoMedia;
  69.     OSType                    myMediaType;
  70.     Boolean                    done = FALSE;
  71.     GWorldPtr                myGWorld;
  72.     Rect                    movieBounds;
  73.     GDHandle                oldGDevice;
  74.     CGrafPtr                oldPort;
  75.     Handle                    compressedData;
  76.     ImageDescriptionHandle    imageDescH;
  77.     TimeValue                sampleDuration;
  78.     
  79.     types[0] = 'MooV';
  80.     StandardGetFilePreview(nil, 1, types, &reply);
  81.     if (!reply.sfGood) return((Movie) 0);
  82.  
  83.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsWrPerm);
  84.     if (GetMoviesError()) return((Movie) 0);
  85.     if (err) return ((Movie) 0);
  86.     
  87.     actualResId = 0;
  88.  
  89.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (StringPtr) 0, newMovieActive, (Boolean *) 0);
  90.     if (GetMoviesError()) return((Movie) 0);
  91.     
  92.     if (err) return ((Movie) 0);
  93.  
  94. /*    This is where I add a frame to the end of the movie's video track.    */
  95.  
  96.     for (i=1; ((i<=GetMovieTrackCount(theMovie)) && (!done)); i++) {
  97.         myVideoTrack = GetMovieIndTrack(theMovie, i);
  98.         myVideoMedia = GetTrackMedia(myVideoTrack);
  99.         GetMediaHandlerDescription(myVideoMedia, &myMediaType, nil, nil);
  100.         if (myMediaType == VideoMediaType)    done = TRUE;
  101.         }
  102.     if (done == FALSE) Error("\PMovie contains no video tracks.", -1);
  103.     else {
  104.         if (BeginMediaEdits(myVideoMedia))
  105.             Error("\PBeginMediaEdits failed.", -1);
  106.     
  107.         GetMovieBox(theMovie, &movieBounds);
  108.         if (NewGWorld(&myGWorld, 1, &movieBounds, nil, nil, 0))    Error("\PNewGWorld failed.", -1);
  109.         else {
  110.         
  111.         GetGWorld(&oldPort, &oldGDevice);
  112.         
  113.         SetGWorld(myGWorld, nil);                        /* Draw the Image... */
  114.         LockPixels(GetGWorldPixMap(myGWorld));
  115.         EraseRect(&movieBounds);
  116.         MoveTo(10,10);    DrawString("\PHi there, John!!!");
  117.         
  118.         imageDescH = (ImageDescriptionHandle) NewHandle(4);
  119.          if (GetMaxCompressionSize(GetGWorldPixMap(myGWorld),
  120.                             &movieBounds,
  121.                             1,
  122.                             codecNormalQuality,
  123.                             'raw ',
  124.                             anyCodec,
  125.                             &maxCompressionSize))
  126.                                 Error("\PCompressImage.", -1);
  127.         compressedData = NewHandle(maxCompressionSize);
  128.         if (compressedData == nil)    Error("\PCould not allocate compressedData block", -1);
  129.         else {
  130.             MoveHHi(compressedData);    HLock(compressedData);
  131.             
  132.             if (err = CompressImage(GetGWorldPixMap(myGWorld),
  133.                                 &movieBounds,
  134.                                 codecNormalQuality,
  135.                                 'raw ',
  136.                                 imageDescH,
  137.                                 StripAddress(*compressedData)))
  138.                 Error("\PCompressImage.", err);
  139.     
  140.             if (err = AddMediaSample(myVideoMedia,
  141.                                 compressedData,
  142.                                 0,
  143.                                 (**imageDescH).dataSize,
  144.                                 GetMediaTimeScale(myVideoMedia),
  145.                                 (SampleDescriptionHandle) imageDescH,
  146.                                 1,
  147.                                 0,
  148.                                 &sampleDuration))
  149.                 Error("\PInsertMediaIntoTracks.", err);
  150.     
  151.             if (err = InsertMediaIntoTrack(myVideoTrack,
  152.                                 GetTrackDuration(myVideoTrack),
  153.                                 sampleDuration,
  154.                                 GetMediaTimeScale(myVideoMedia),
  155.                                 0x00010000))
  156.                 Error("\PInsertMediaIntoTracks.", err);
  157.     
  158.             if (err = EndMediaEdits(myVideoMedia))    Error("\PEndMediaEdits failed.", err);
  159.             
  160.             SetGWorld(oldPort, oldGDevice);
  161.             HUnlock(compressedData);
  162.             DisposeHandle(compressedData);
  163.             }
  164.         DisposeHandle(imageDescH);
  165.         
  166.         DisposeGWorld(myGWorld);
  167.         }
  168.     }
  169.     
  170.     if (err = UpdateMovieResource(theMovie, movieResRefNum, actualResId, nil))
  171.         Error("\PUpdateResource failed.", err);
  172.  
  173. /*    This is the end of the code for adding the one frame to the movie.    */
  174.  
  175.     err = CloseMovieFile(movieResRefNum);
  176.     if (GetMoviesError()) return((Movie) 0);
  177.     if (err) return ((Movie) 0);
  178.  
  179.     return (theMovie);
  180. }
  181.  
  182. /*------------------------------------------------------*/
  183. /*    playMovie().                                            */
  184. /*------------------------------------------------------*/
  185.  
  186. OSErr playMovie(int    index)
  187. {
  188.     Rect            movieBounds;
  189.     
  190.     GetMovieBox(myMovie[index], &movieBounds);
  191.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  192.     if (movieBounds.right < 40) movieBounds.right = 40;
  193.     if (movieBounds.bottom < 20) movieBounds.bottom = 20;
  194.     SetMovieBox(myMovie[index], &movieBounds);
  195.     OffsetRect(&movieBounds, startlocation, startlocation);
  196.     myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, TRUE, 0L);
  197.     startlocation += 50;
  198.     if (startlocation > 300) startlocation = 50;
  199.     
  200.     SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
  201.     if (GetMoviesError()) Error("\PSetMovieGWorld error.", GetMoviesError());
  202.  
  203. /*    Uncomment these lines if you want to pre load the movie into ram.
  204.     GotoBeginningOfMovie(myMovie[index]);
  205.     if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  206.                                     GetMovieDuration(myMovie[index]), 0) != noErr)
  207.         Error("\PNot enough memory to load movie into ram.", -1);
  208. */
  209.     SetMovieRate(myMovie[index], 0x00010000);
  210.  
  211. }
  212.  
  213. /*------------------------------------------------------*/
  214. /*    flatten().                                            */
  215. /*------------------------------------------------------*/
  216.  
  217. short flatten(Movie    myMovie)
  218. {    StandardFileReply        reply;
  219.     OSErr       theErr      = noErr;
  220.  
  221.     StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
  222.     if (!reply.sfGood) return;
  223.  
  224.      if (theErr = GetMoviesError())    Error("\PCall Before FlattenMovies failed.", theErr);
  225.  
  226.     FlattenMovie(myMovie,
  227.                 flattenAddMovieToDataFork,
  228.                 &reply.sfFile,
  229.                 'JWJW',
  230.                 0,
  231.                 createMovieFileDeleteCurFile,
  232.                 nil,
  233.                 nil);
  234.                 
  235.     if (theErr = GetMoviesError())    Error("\PFlattenMovies failed.",theErr);
  236.     return(theErr);
  237. }
  238.  
  239. /*------------------------------------------------------*/
  240. /*    doFlattenCommand().                                    */
  241. /*------------------------------------------------------*/
  242. void doFlattenCommand()
  243. {    int            i;
  244.     WindowPtr    myTempWindow;
  245.     
  246.     /*    Flatten movie that is currently selected.    */
  247.     myTempWindow = FrontWindow();
  248.     if (myTempWindow == nil) return;
  249.     
  250.     for (i = 0; i < MAXWINDOWS; i++)
  251.         if (myWindow[i] == myTempWindow) {
  252.             flatten(myMovie[i]);
  253.             break;
  254.             }
  255. }
  256.  
  257. /*------------------------------------------------------*/
  258. /*    doOpenCommand().                                    */
  259. /*------------------------------------------------------*/
  260. void doOpenCommand()
  261. {    int        i;
  262.     
  263.     /*    Search for the first window that is nil.    */
  264.     for (i = MAXWINDOWS-1; i >= 0; i--)
  265.         if (myWindow[i] == nil) {
  266.             myMovie[useThisIndex] = getMovieFromFile();
  267.             if (myMovie[useThisIndex] != 0) {
  268.                 playMovie(useThisIndex);
  269.                 playingMovie[useThisIndex] = TRUE;
  270.                 }
  271.             break;
  272.             }
  273. }
  274.  
  275. /*------------------------------------------------------*/
  276. /*    playMovies().                                            */
  277. /*------------------------------------------------------*/
  278. playMovies()
  279. {    int        i;
  280.     
  281.     for (i = 0; i < MAXWINDOWS; i++)
  282.         if (playingMovie[i] == TRUE) {
  283.             if (IsMovieDone(myMovie[i]) == FALSE)
  284.                 MoviesTask(myMovie[i], DoTheRightThing);
  285.         }
  286. }
  287.  
  288. /*------------------------------------------------------*/
  289. /*    doCloseCommand().                                    */
  290. /*------------------------------------------------------*/
  291. void doCloseCommand()
  292. {    int        i, useThisIndex;
  293.     WindowPtr    myTempWindow;
  294.                                     
  295.     myTempWindow = FrontWindow();        /*    Close selected window.    */
  296.     if (myTempWindow == nil) return;
  297.     for (i = 0; i < MAXWINDOWS; i++)
  298.         if (myWindow[i] == myTempWindow) {
  299.             DisposeMovie(myMovie[i]);
  300.             DisposeWindow(myTempWindow);
  301.             playingMovie[i] = FALSE;
  302.             myWindow[i] = nil;
  303.         }
  304. }
  305.  
  306. /*------------------------------------------------------*/
  307. /*    doCommand().                                        */
  308. /*------------------------------------------------------*/
  309. void doCommand(long    mResult)
  310. {    int         theMenu, theItem;
  311.     char        daName[256];
  312.     GrafPtr     savePort;
  313.  
  314.     theItem = LoWord(mResult);    theMenu = HiWord(mResult);
  315.     switch (theMenu) {
  316.         case appleID:
  317.             if (theItem == aboutMeCommand)    showAboutMeDialog();
  318.             else {
  319.                 GetItem(mymenu0, theItem, daName);
  320.                 GetPort(&savePort);    (void) OpenDeskAcc(daName);
  321.                 SetPort(savePort);
  322.             }
  323.             break;
  324.  
  325.         case fileID:
  326.             switch (theItem) {
  327.                 case openCommand:        doOpenCommand();    break;
  328.                 case flattenCommand:    doFlattenCommand();    break;
  329.                 case closeCommand:        doCloseCommand();    break;
  330.                 case quitCommand:        DoneFlag = TRUE;    break;
  331.                 }
  332.             break;
  333.         }
  334.     HiliteMenu(0);
  335.     return;
  336. }
  337.  
  338. /*------------------------------------------------------*/
  339. /*    init().                                                */
  340. /*------------------------------------------------------*/
  341. void init()
  342. {    OSErr                err;
  343.     int                    i;
  344.     long                QDfeature, OSfeature;
  345.  
  346.     /*    Initialize Managaer.    */
  347.     InitGraf(&qd.thePort);
  348.     FlushEvents(everyEvent, 0);
  349.     InitWindows();    InitDialogs(nil);    InitCursor();
  350.  
  351.     /*    Set up menus.    */
  352.     mymenu0 = GetMenu(appleID);    AddResMenu(mymenu0, 'DRVR');    InsertMenu(mymenu0,0);
  353.     mymenu1 = GetMenu(fileID);    InsertMenu(mymenu1,0);
  354.     DrawMenuBar();
  355.     
  356.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  357.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  358.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  359.         if (!err)    err = Gestalt(gestaltSystemVersion, &OSfeature);
  360.         if (!err)    {        /* Check if Color QuickDraw & System Version 6.07 or later? */
  361.             if ((QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607) ExitToShell();
  362.             err = Gestalt(gestaltQuickTime, &QDfeature);
  363.             }
  364.         if (err)    ExitToShell();
  365.     } else    ExitToShell();
  366.         
  367.     startlocation = 50;            /*    Set up variables.    */
  368.     for (i = 0; i < MAXWINDOWS; i++) {
  369.         playingMovie[i] = FALSE;
  370.         myWindow[i] = nil;
  371.         }
  372.         
  373.     /*    Open QuickTime last.    */
  374.     err = EnterMovies();
  375.     if (err)    ExitToShell();
  376. }
  377.  
  378. /*------------------------------------------------------*/
  379. /*    finish().                                            */
  380. /*------------------------------------------------------*/
  381. void finish()
  382. {
  383.     ExitMovies();
  384.     ExitToShell();
  385. }
  386.  
  387. /*------------------------------------------------------*/
  388. /*    showAboutMeDialog()                                    */
  389. /*------------------------------------------------------*/
  390. void showAboutMeDialog()
  391. {    GrafPtr     savePort;
  392.     DialogPtr    theDialog;
  393.     short        itemHit;
  394.  
  395.     GetPort(&savePort);
  396.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  397.     SetPort(theDialog);
  398.  
  399.     do {
  400.         ModalDialog(nil, &itemHit);
  401.     } while (itemHit != okButton);
  402.  
  403.     CloseDialog(theDialog);    SetPort(savePort);
  404.     return;
  405. }
  406.  
  407. /*------------------------------------------------------*/
  408. /*    main().                                                */
  409. /*------------------------------------------------------*/
  410.  
  411. main()
  412. {    int                    i;
  413.     char            key;
  414.     Boolean            track;
  415.     long            growResult;
  416.     EventRecord     myEvent;
  417.     WindowPtr        whichWindow;
  418.     int                yieldTime;
  419.  
  420.     init();
  421.     
  422.     yieldTime = 0;
  423.     for ( ;; ) {
  424.     
  425.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  426.         if (DoneFlag)    finish();    /* Call's ExitToShell()! */
  427.             
  428.         /*    Play movies which are active.    */
  429.         playMovies();
  430.  
  431.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  432.             switch (myEvent.what) {
  433.                 case mouseDown:
  434.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  435.                         case inSysWindow:    SystemClick(&myEvent, whichWindow);
  436.                             break;
  437.                         case inMenuBar:        doCommand(MenuSelect(myEvent.where));
  438.                             break;
  439.                         case inContent:        SelectWindow(whichWindow);
  440.                             break;
  441.                         case inDrag:        DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  442.                             break;
  443.                         case inGoAway:
  444.                             track = TrackGoAway (whichWindow, myEvent.where);
  445.                             if (track)    doCloseCommand();
  446.                             break;
  447.                         case inGrow:    case inZoomIn:    case inZoomOut:    break;
  448.                     }
  449.                     break;
  450.                 case keyDown:
  451.                 case autoKey:
  452.                     key = myEvent.message & charCodeMask;
  453.                     if ( myEvent.modifiers & cmdKey )
  454.                         if ( myEvent.what == keyDown )    doCommand(MenuKey(key));
  455.                     break;
  456.                 case updateEvt:
  457.                     for (i = 0; i < MAXWINDOWS; i++)
  458.                         if ((WindowPtr) myEvent.message == myWindow[i]) {
  459.                             BeginUpdate((WindowPtr) myWindow[i]);
  460.                             EndUpdate((WindowPtr) myWindow[i]);
  461.                         }
  462.                     break;
  463.                 case diskEvt:        break;
  464.                 case activateEvt:    break;
  465.                 case app4Evt:        break; 
  466.             }
  467.         }
  468.     }
  469. }